<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>天气预报查询</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        table {
            display: none;
            border-collapse: collapse;
            width: 100%;
            margin-top: 20px;
            text-align: center;
        }

        tr {
            height: 50px;
        }

        tr td {
            border: 1px solid black;
            width: 200px;
        }
    </style>
</head>

<body>
    <input type="text" placeholder="请输入城市">
    <button>天气查询</button>
    <table>
        <!-- <tr>
            <td>1</td>
            <td>1</td>
            <td>1</td>
            <td>1</td>
        </tr> -->
    </table>
</body>
<script>
    let tex = document.querySelector('input');
    let btn = document.querySelector('button');
    let table = document.querySelector('table');

    btn.onclick = function () {
        let xhr = new XMLHttpRequest();
        let url = '../service/weather.php?city=' + tex.value;
        xhr.open('GET', url);
        xhr.send();
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && xhr.status == 200) {
                console.log(JSON.parse(xhr.responseText));
                
                let data = JSON.parse(xhr.responseText);
                console.log(data.result.data.weather);
                console.log(data.result.data.weather[0].date);
                let htmlStr = '';
                for (let i = 0; i < data.result.data.weather.length; i++) {
                    htmlStr += `<tr>
                                    <td>${data.result.data.weather[i].date}</td>
                                    <td>${data.result.data.weather[i].info.day[1]}</td>
                                    <td>气温:${data.result.data.weather[i].info.day[2]}度</td>
                                    <td>${data.result.data.weather[i].info.day[3]}&nbsp;${data.result.data.weather[i].info.day[4]}</td>
                                    <td>周${data.result.data.weather[i].week}</td>
                                </tr>`;
                }
                table.innerHTML = htmlStr;
                table.style.display = 'block';
            }
        }
    }

</script>

</html>